package gui.VPOfOperations;

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import javax.swing.border.TitledBorder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

import javax.swing.JLabel;

import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

import entities.DashboardGauges;
import entities.FailReport;
import entities.WIP;
import utilities.DateLabelFormatter;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.ImageIcon;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;


/**
 * @author Idan
 * GUI for the Dashboard.
 */
public class VPOfOperationsDashboard extends JPanel
{
	public JPanel dashboardPanel;
	public JPanel engraveGaugePanel;
	public JPanel polishGaugePanel;
	public JPanel qcGaugePanel;
	public JPanel packagingGaugePanel;
	private JTextField engravePlanTextField;
	private JTextField engraveDoneTextField;
	private JTextField engraveYesterdayTextField;
	private JTextField polishPlanTextField;
	private JTextField polishDoneTextField;
	private JTextField polishYesterdayTextField;
	private JTextField qualityPlanTextField;
	private JTextField qualityDoneTextField;
	private JTextField qualityYesterdayTextField;
	private JTextField packagingPlanTextField;
	private JTextField packagingDoneTextField;
	private JTextField packagingYesterdayTextField;
	private JTextField wipEngraveTextField;
	private JTextField wipPolishTextField;
	private JTextField wipQualityTextField;
	private JTextField wipPackagingTextField;
	private JTextField yesterdayQualityTotalTextField;
	private JTextField yesterdayQualityPercentageTextField;
	private JTextField cummulatativeQualityTotalTextField;
	private JTextField cummulatativeQualityPercentageTextField;
	private JTextField yesterdayHardCountTextField;
	private JTextField yesterdayHydrophobCountTextField;
	private JTextField yesterdayHydrophilCountTextField;
	private JTextField yesterdayPresbiaCountTextField;
	private JTextField yesterdayPresbiaPercentageTextField;
	private JTextField yesterdayHydrophilPercentageTextField;
	private JTextField yesterdayHydrophobPercentageTextField;
	private JTextField yesterdayHardPercentageTextField;
	private JTextField totalHardCountTextField;
	private JTextField totalHydrophobCountTextField;
	private JTextField totalHydrophilCountTextField;
	private JTextField totalPresbiaCountTextField;
	private JTextField totalPresbiaPercentageTextField;
	private JTextField totalHydrophilPercentageTextField;
	private JTextField totalHydrophobPercentageTextField;
	private JTextField totalHardPercentageTextField;
	private UtilDateModel startDate;
	private UtilDateModel endDate;
	private UtilDateModel todayDate;
	private JButton exitButton;
	private VPOfOperationsDashboard myself;
	
	
	public VPOfOperationsDashboard()
	{
		setBackground(new Color(173, 216, 230));
		this.myself = this;
		setLayout(new MigLayout("", "[grow][grow][grow]", "[][grow][]"));
		
		
		
		// setup date pickers
		
		UtilDateModel startmodel = new UtilDateModel();
		UtilDateModel endmodel = new UtilDateModel();
		
		
		Properties p = new Properties();
		p.put("text.today", "היום");
		p.put("text.month", "חודש");
		p.put("text.year", "שנה");
		JDatePanelImpl startPanel = new JDatePanelImpl(startmodel, p);
		JDatePanelImpl endPanel = new JDatePanelImpl(endmodel, p);
		

		JDatePickerImpl datePickerstart = new JDatePickerImpl(startPanel, new DateLabelFormatter());
		datePickerstart.getJFormattedTextField().setFont(new Font("Tahoma", Font.PLAIN, 13));
		datePickerstart.setBackground(new Color(173, 216, 230));
		datePickerstart.getJFormattedTextField().setHorizontalAlignment(SwingConstants.CENTER);
		JDatePickerImpl datePickerend = new JDatePickerImpl(endPanel, new DateLabelFormatter());
		datePickerend.getJFormattedTextField().setFont(new Font("Tahoma", Font.PLAIN, 13));
		datePickerend.setBackground(new Color(173, 216, 230));
		datePickerend.getJFormattedTextField().setHorizontalAlignment(SwingConstants.CENTER);
		
		
		
		// set first day of the month the default start date
		
		startmodel.setDate(startmodel.getYear(), startmodel.getMonth(), 1);
				
		startmodel.setSelected(true);
		endmodel.setSelected(true);
		JButton refreshButton = new JButton("רענן");
		refreshButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
		refreshButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		refreshButton.setIcon(new ImageIcon(VPOfOperationsDashboard.class.getResource("/graphics/refresh16.png")));
		refreshButton.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				if (datePickerend.getJFormattedTextField().getText().equals("") || datePickerstart.getJFormattedTextField().getText().equals(""))
				{
					JOptionPane.showMessageDialog(dashboardPanel,
						    "טווח תאריכים שנבחר אינו תקין.","שגיאה",JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					
					
					// make sure end date is after start date
					
					ArrayList<Object> datesForDashBoard = getDatesForDashboard();
					
					Timestamp startDate = new Timestamp ( ((Date)datesForDashBoard.get(0)).getTime() );
					Timestamp endDate = new Timestamp ( ((Date)datesForDashBoard.get(1)).getTime() );
					Timestamp yesterday = new Timestamp ( ((Date)datesForDashBoard.get(2)).getTime() );
					
					
					
					if (startDate.after(endDate))
					{
						JOptionPane.showMessageDialog(dashboardPanel,
							    "טווח תאריכים שנבחר אינו תקין.","שגיאה",JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						ArrayList<Object> getDashboardData = new ArrayList<Object>();
						getDashboardData.add("getValuesForDashboard");
						
						// add start date
						getDashboardData.add(startDate);
						// add end date
						getDashboardData.add(endDate);
						// add yesterday date
						getDashboardData.add(yesterday);
	
						ArrayList<Object> response;
						try 
						{
							response = VPOfOperationsMain.voopClient.call(getDashboardData);
							setVariables((ArrayList<Object>)response);
							
						} 
						catch (Exception e)
						{
							
							//e.printStackTrace();
						}
						
					}
				}
			}
		});
		add(refreshButton, "flowx,cell 1 0,alignx center");
		
		
		add(datePickerend, "cell 1 0,alignx center");
		
		JLabel setEndDatelabel = new JLabel("עד-");
		setEndDatelabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(setEndDatelabel, "cell 1 0,alignx center");
		
		JLabel space = new JLabel("        ");
		add(space, "cell 1 0,alignx center");
		
		
		add(datePickerstart, "cell 1 0,alignx center");
		
		JLabel setStartDateLabel = new JLabel("מ-");
		setStartDateLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(setStartDateLabel, "cell 1 0,alignx center");
		
		startDate = startmodel;
		endDate = endmodel;
		

		JPanel mainDashboardPanel = new JPanel(){	@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		mainDashboardPanel.setBackground(new Color(173, 216, 230));
		mainDashboardPanel.setBorder(new TitledBorder(null, "Dashboard", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(59, 59, 59)));
		add(mainDashboardPanel, "cell 1 1,grow");
		mainDashboardPanel.setLayout(new MigLayout("", "[grow]", "[grow][grow][grow][grow][]"));
		
		dashboardPanel = new JPanel();
		dashboardPanel.setBackground(new Color(173, 216, 230));
		dashboardPanel.setPreferredSize(new Dimension(500, 350));
		mainDashboardPanel.add(dashboardPanel, "cell 0 0,growx,aligny top");
		dashboardPanel.setBorder(new TitledBorder(null, "\u05EA\u05E4\u05D5\u05E7\u05D5\u05EA", TitledBorder.RIGHT, TitledBorder.TOP, null, new Color(59, 59, 59)));
		dashboardPanel.setLayout(new MigLayout("", "[grow][][50px,grow][][50px,grow][][50px,grow][][50px,grow][][grow]", "[][][][][][][][]"));
		
		JLabel packagingLabel = new JLabel("אריזה");
		packagingLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(packagingLabel, "cell 2 0,alignx center");
		
		JLabel polishLabel = new JLabel("ליטוש");
		polishLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(polishLabel, "cell 6 0,alignx center");
		
		packagingGaugePanel = new JPanel();
		packagingGaugePanel.setBackground(new Color(173, 216, 230));
		dashboardPanel.add(packagingGaugePanel, "cell 2 1,alignx center,aligny center");
		packagingGaugePanel.setLayout(new BorderLayout(0, 0));
		packagingGaugePanel.setPreferredSize(new Dimension(185, 185));
		
		qcGaugePanel = new JPanel();
		qcGaugePanel.setBackground(new Color(173, 216, 230));
		dashboardPanel.add(qcGaugePanel, "cell 4 1,alignx center,aligny center");
		qcGaugePanel.setLayout(new BorderLayout(0, 0));
		qcGaugePanel.setPreferredSize(new Dimension(185, 185));
		
		polishGaugePanel = new JPanel();
		polishGaugePanel.setBackground(new Color(173, 216, 230));
		dashboardPanel.add(polishGaugePanel, "cell 6 1,alignx center,aligny center");
		polishGaugePanel.setLayout(new BorderLayout(0, 0));
		polishGaugePanel.setPreferredSize(new Dimension(185, 185));
		
		engraveGaugePanel = new JPanel();
		engraveGaugePanel.setBackground(new Color(173, 216, 230));
		dashboardPanel.add(engraveGaugePanel, "cell 8 1,alignx center,aligny center");
		engraveGaugePanel.setLayout(new BorderLayout(0, 0));
		engraveGaugePanel.setPreferredSize(new Dimension(185, 185));
		
		JLabel label = new JLabel("ביצוע \\ תכנון");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label, "cell 2 2,alignx center");
		
		JLabel label_11 = new JLabel("ביצוע \\ תכנון");
		label_11.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_11, "cell 4 2,alignx center");
		
		JLabel label_12 = new JLabel("ביצוע \\ תכנון");
		label_12.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_12, "cell 6 2,alignx center");
		
		JLabel label_13 = new JLabel("ביצוע \\ תכנון");
		label_13.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_13, "cell 8 2,alignx center");
		
		packagingPlanTextField = new JTextField();
		packagingPlanTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		packagingPlanTextField.setHorizontalAlignment(SwingConstants.CENTER);
		packagingPlanTextField.setEditable(false);
		packagingPlanTextField.setColumns(6);
		dashboardPanel.add(packagingPlanTextField, "flowx,cell 2 3,alignx center");
		
		packagingDoneTextField = new JTextField();
		packagingDoneTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		packagingDoneTextField.setHorizontalAlignment(SwingConstants.CENTER);
		packagingDoneTextField.setEditable(false);
		packagingDoneTextField.setColumns(6);
		dashboardPanel.add(packagingDoneTextField, "cell 2 3,alignx center");
		
		qualityPlanTextField = new JTextField();
		qualityPlanTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPlanTextField.setHorizontalAlignment(SwingConstants.CENTER);
		qualityPlanTextField.setEditable(false);
		qualityPlanTextField.setColumns(6);
		dashboardPanel.add(qualityPlanTextField, "flowx,cell 4 3,alignx center");
		
		qualityDoneTextField = new JTextField();
		qualityDoneTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityDoneTextField.setHorizontalAlignment(SwingConstants.CENTER);
		qualityDoneTextField.setEditable(false);
		qualityDoneTextField.setColumns(6);
		dashboardPanel.add(qualityDoneTextField, "cell 4 3,alignx center");
		
		JLabel label_1 = new JLabel("בוצע אתמול");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_1, "cell 2 4,alignx center");
		
		JLabel label_4 = new JLabel("בוצע אתמול");
		label_4.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_4, "cell 4 4,alignx center");
		
		JLabel label_7 = new JLabel("בוצע אתמול");
		label_7.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_7, "cell 6 4,alignx center");
		
		JLabel label_10 = new JLabel("בוצע אתמול");
		label_10.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(label_10, "cell 8 4,alignx center");
		
		packagingYesterdayTextField = new JTextField();
		packagingYesterdayTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		packagingYesterdayTextField.setHorizontalAlignment(SwingConstants.CENTER);
		packagingYesterdayTextField.setEditable(false);
		packagingYesterdayTextField.setColumns(5);
		dashboardPanel.add(packagingYesterdayTextField, "cell 2 5,alignx center");
		
		JLabel engraveLabel = new JLabel("ייצור");
		engraveLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(engraveLabel, "cell 8 0,alignx center");
		
		JLabel qcLabel = new JLabel("איכות");
		qcLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dashboardPanel.add(qcLabel, "cell 4 0,alignx center");
		
		engravePlanTextField = new JTextField();
		engravePlanTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		engravePlanTextField.setEditable(false);
		engravePlanTextField.setHorizontalAlignment(SwingConstants.CENTER);
		dashboardPanel.add(engravePlanTextField, "flowx,cell 8 3,alignx center");
		engravePlanTextField.setColumns(6);
		
		polishPlanTextField = new JTextField();
		polishPlanTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		polishPlanTextField.setHorizontalAlignment(SwingConstants.CENTER);
		polishPlanTextField.setEditable(false);
		polishPlanTextField.setColumns(6);
		dashboardPanel.add(polishPlanTextField, "flowx,cell 6 3,alignx center");
		
		polishDoneTextField = new JTextField();
		polishDoneTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		polishDoneTextField.setHorizontalAlignment(SwingConstants.CENTER);
		polishDoneTextField.setEditable(false);
		polishDoneTextField.setColumns(6);
		dashboardPanel.add(polishDoneTextField, "cell 6 3,alignx center");
		
		engraveDoneTextField = new JTextField();
		engraveDoneTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		engraveDoneTextField.setEditable(false);
		engraveDoneTextField.setHorizontalAlignment(SwingConstants.CENTER);
		dashboardPanel.add(engraveDoneTextField, "cell 8 3,alignx center");
		engraveDoneTextField.setColumns(6);
		
		polishYesterdayTextField = new JTextField();
		polishYesterdayTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		polishYesterdayTextField.setHorizontalAlignment(SwingConstants.CENTER);
		polishYesterdayTextField.setEditable(false);
		polishYesterdayTextField.setColumns(5);
		dashboardPanel.add(polishYesterdayTextField, "cell 6 5,alignx center");
		
		engraveYesterdayTextField = new JTextField();
		engraveYesterdayTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		engraveYesterdayTextField.setEditable(false);
		engraveYesterdayTextField.setHorizontalAlignment(SwingConstants.CENTER);
		dashboardPanel.add(engraveYesterdayTextField, "cell 8 5,alignx center");
		engraveYesterdayTextField.setColumns(5);
		
		qualityYesterdayTextField = new JTextField();
		qualityYesterdayTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityYesterdayTextField.setHorizontalAlignment(SwingConstants.CENTER);
		qualityYesterdayTextField.setEditable(false);
		qualityYesterdayTextField.setColumns(5);
		dashboardPanel.add(qualityYesterdayTextField, "cell 4 5,alignx center");
		
		JPanel wipPanel = new JPanel(){	@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		wipPanel.setBackground(new Color(173, 216, 230));
		wipPanel.setBorder(new TitledBorder(null, "WIP", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		mainDashboardPanel.add(wipPanel, "cell 0 1,growx,aligny center");
		wipPanel.setLayout(new MigLayout("", "[grow][][50px,grow][][50px,grow][][50px,grow][][50px,grow][][grow]", "[][][]"));
		wipPanel.setPreferredSize(new Dimension(500, 110));
		
		JLabel label_6 = new JLabel("ממתינות באריזה");
		label_6.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPanel.add(label_6, "cell 2 0,alignx center");
		
		JLabel label_5 = new JLabel("ממתינות בבקרה");
		label_5.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPanel.add(label_5, "cell 4 0,alignx center");
		
		wipPackagingTextField = new JTextField();
		wipPackagingTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPackagingTextField.setEditable(false);
		wipPackagingTextField.setHorizontalAlignment(SwingConstants.CENTER);
		wipPackagingTextField.setColumns(6);
		wipPanel.add(wipPackagingTextField, "cell 2 1,alignx center");
		
		wipPolishTextField = new JTextField();
		wipPolishTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPolishTextField.setEditable(false);
		wipPolishTextField.setHorizontalAlignment(SwingConstants.CENTER);
		wipPolishTextField.setColumns(6);
		wipPanel.add(wipPolishTextField, "cell 6 1,alignx center");
		
		wipEngraveTextField = new JTextField();
		wipEngraveTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipEngraveTextField.setEditable(false);
		wipEngraveTextField.setHorizontalAlignment(SwingConstants.CENTER);
		wipPanel.add(wipEngraveTextField, "flowx,cell 8 1,alignx center");
		wipEngraveTextField.setColumns(6);
		
		JLabel label_2 = new JLabel("ממתינות בייצור");
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPanel.add(label_2, "cell 8 0,alignx center");
		
		JLabel label_3 = new JLabel("ממתינות בליטוש");
		label_3.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipPanel.add(label_3, "cell 6 0,alignx center");
		
		wipQualityTextField = new JTextField();
		wipQualityTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		wipQualityTextField.setEditable(false);
		wipQualityTextField.setHorizontalAlignment(SwingConstants.CENTER);
		wipQualityTextField.setColumns(6);
		wipPanel.add(wipQualityTextField, "cell 4 1,alignx center");
		
		JPanel qualityPanel = new JPanel(){	@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		qualityPanel.setBackground(new Color(173, 216, 230));
		qualityPanel.setBorder(new TitledBorder(null, "\u05D0\u05D9\u05DB\u05D5\u05EA", TitledBorder.RIGHT, TitledBorder.TOP, null, new Color(59, 59, 59)));
		qualityPanel.setPreferredSize(new Dimension (500,350));
		mainDashboardPanel.add(qualityPanel, "cell 0 2,grow");
		qualityPanel.setLayout(new MigLayout("", "[grow][][][][][grow][][][][][grow]", "[grow][][][][][][][grow]"));
		
		JLabel label_9 = new JLabel("מצטבר");
		label_9.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_9, "cell 3 0,alignx center");
		
		JLabel label_8 = new JLabel("אתמול");
		label_8.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_8, "cell 8 0,alignx center");
		
		cummulatativeQualityPercentageTextField = new JTextField();
		cummulatativeQualityPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		cummulatativeQualityPercentageTextField.setEditable(false);
		cummulatativeQualityPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		cummulatativeQualityPercentageTextField.setColumns(4);
		qualityPanel.add(cummulatativeQualityPercentageTextField, "cell 1 1,alignx center");
		
		JLabel label_17 = new JLabel("אחוז ");
		label_17.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_17, "cell 2 1,alignx center");
		
		JLabel label_16 = new JLabel("סה\"כ פסילות");
		label_16.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_16, "cell 4 1,alignx center");
		
		yesterdayQualityPercentageTextField = new JTextField();
		yesterdayQualityPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayQualityPercentageTextField.setEditable(false);
		yesterdayQualityPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayQualityPercentageTextField.setColumns(4);
		qualityPanel.add(yesterdayQualityPercentageTextField, "flowx,cell 6 1,alignx center");
		
		JLabel label_15 = new JLabel("אחוז ");
		label_15.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_15, "cell 7 1,alignx center");
		
		yesterdayQualityTotalTextField = new JTextField();
		yesterdayQualityTotalTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayQualityTotalTextField.setEditable(false);
		yesterdayQualityTotalTextField.setHorizontalAlignment(SwingConstants.CENTER);
		qualityPanel.add(yesterdayQualityTotalTextField, "flowx,cell 8 1,alignx center");
		yesterdayQualityTotalTextField.setColumns(6);
		
		cummulatativeQualityTotalTextField = new JTextField();
		cummulatativeQualityTotalTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		cummulatativeQualityTotalTextField.setEditable(false);
		cummulatativeQualityTotalTextField.setHorizontalAlignment(SwingConstants.CENTER);
		cummulatativeQualityTotalTextField.setColumns(6);
		qualityPanel.add(cummulatativeQualityTotalTextField, "flowx,cell 3 1,alignx center");
		
		JLabel label_14 = new JLabel("סה\"כ פסילות");
		label_14.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_14, "cell 9 1,alignx center");
		
		totalHardPercentageTextField = new JTextField();
		totalHardPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHardPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHardPercentageTextField.setEditable(false);
		totalHardPercentageTextField.setColumns(4);
		qualityPanel.add(totalHardPercentageTextField, "cell 1 3,alignx center");
		
		totalHardCountTextField = new JTextField();
		totalHardCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHardCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHardCountTextField.setEditable(false);
		totalHardCountTextField.setColumns(6);
		qualityPanel.add(totalHardCountTextField, "cell 3 3,alignx center");
		
		JLabel label_22 = new JLabel("קשות");
		label_22.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_22, "cell 4 3,alignx center");
		
		yesterdayHardPercentageTextField = new JTextField();
		yesterdayHardPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHardPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHardPercentageTextField.setEditable(false);
		yesterdayHardPercentageTextField.setColumns(4);
		qualityPanel.add(yesterdayHardPercentageTextField, "cell 6 3,alignx center");
		
		yesterdayHardCountTextField = new JTextField();
		yesterdayHardCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHardCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHardCountTextField.setEditable(false);
		yesterdayHardCountTextField.setColumns(6);
		qualityPanel.add(yesterdayHardCountTextField, "cell 8 3,alignx center");
		
		JLabel label_18 = new JLabel("קשות");
		label_18.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_18, "cell 9 3,alignx center");
		
		totalHydrophobPercentageTextField = new JTextField();
		totalHydrophobPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHydrophobPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHydrophobPercentageTextField.setEditable(false);
		totalHydrophobPercentageTextField.setColumns(4);
		qualityPanel.add(totalHydrophobPercentageTextField, "cell 1 4,alignx center");
		
		totalHydrophobCountTextField = new JTextField();
		totalHydrophobCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHydrophobCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHydrophobCountTextField.setEditable(false);
		totalHydrophobCountTextField.setColumns(6);
		qualityPanel.add(totalHydrophobCountTextField, "cell 3 4,alignx center");
		
		JLabel label_23 = new JLabel("הידרופוביות");
		label_23.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_23, "cell 4 4,alignx center");
		
		yesterdayHydrophobPercentageTextField = new JTextField();
		yesterdayHydrophobPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHydrophobPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHydrophobPercentageTextField.setEditable(false);
		yesterdayHydrophobPercentageTextField.setColumns(4);
		qualityPanel.add(yesterdayHydrophobPercentageTextField, "cell 6 4,alignx center");
		
		yesterdayHydrophobCountTextField = new JTextField();
		yesterdayHydrophobCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHydrophobCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHydrophobCountTextField.setEditable(false);
		yesterdayHydrophobCountTextField.setColumns(6);
		qualityPanel.add(yesterdayHydrophobCountTextField, "cell 8 4,alignx center");
		
		JLabel label_19 = new JLabel("הידרופוביות");
		label_19.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_19, "cell 9 4,alignx center");
		
		totalHydrophilPercentageTextField = new JTextField();
		totalHydrophilPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHydrophilPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHydrophilPercentageTextField.setEditable(false);
		totalHydrophilPercentageTextField.setColumns(4);
		qualityPanel.add(totalHydrophilPercentageTextField, "cell 1 5,alignx center");
		
		totalHydrophilCountTextField = new JTextField();
		totalHydrophilCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalHydrophilCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalHydrophilCountTextField.setEditable(false);
		totalHydrophilCountTextField.setColumns(6);
		qualityPanel.add(totalHydrophilCountTextField, "cell 3 5,alignx center");
		
		JLabel label_24 = new JLabel("הידרופיליות");
		label_24.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_24, "cell 4 5");
		
		yesterdayHydrophilPercentageTextField = new JTextField();
		yesterdayHydrophilPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHydrophilPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHydrophilPercentageTextField.setEditable(false);
		yesterdayHydrophilPercentageTextField.setColumns(4);
		qualityPanel.add(yesterdayHydrophilPercentageTextField, "cell 6 5,alignx center");
		
		yesterdayHydrophilCountTextField = new JTextField();
		yesterdayHydrophilCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayHydrophilCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayHydrophilCountTextField.setEditable(false);
		yesterdayHydrophilCountTextField.setColumns(6);
		qualityPanel.add(yesterdayHydrophilCountTextField, "cell 8 5,alignx center");
		
		JLabel label_20 = new JLabel("הידרופיליות");
		label_20.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_20, "cell 9 5,alignx center");
		
		totalPresbiaPercentageTextField = new JTextField();
		totalPresbiaPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalPresbiaPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalPresbiaPercentageTextField.setEditable(false);
		totalPresbiaPercentageTextField.setColumns(4);
		qualityPanel.add(totalPresbiaPercentageTextField, "cell 1 6,alignx center");
		
		totalPresbiaCountTextField = new JTextField();
		totalPresbiaCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		totalPresbiaCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		totalPresbiaCountTextField.setEditable(false);
		totalPresbiaCountTextField.setColumns(6);
		qualityPanel.add(totalPresbiaCountTextField, "cell 3 6,alignx center");
		
		JLabel label_25 = new JLabel("פרסביה");
		label_25.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_25, "cell 4 6");
		
		yesterdayPresbiaPercentageTextField = new JTextField();
		yesterdayPresbiaPercentageTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayPresbiaPercentageTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayPresbiaPercentageTextField.setEditable(false);
		yesterdayPresbiaPercentageTextField.setColumns(4);
		qualityPanel.add(yesterdayPresbiaPercentageTextField, "cell 6 6,alignx center");
		
		yesterdayPresbiaCountTextField = new JTextField();
		yesterdayPresbiaCountTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		yesterdayPresbiaCountTextField.setHorizontalAlignment(SwingConstants.CENTER);
		yesterdayPresbiaCountTextField.setEditable(false);
		yesterdayPresbiaCountTextField.setColumns(6);
		qualityPanel.add(yesterdayPresbiaCountTextField, "cell 8 6,alignx center");
		
		JLabel label_21 = new JLabel("פרסביה");
		label_21.setFont(new Font("Tahoma", Font.PLAIN, 13));
		qualityPanel.add(label_21, "cell 9 6,alignx center");
		
		exitButton = new JButton("יציאה מהמערכת");
		exitButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
		exitButton.setIcon(new ImageIcon(VPOfOperationsDashboard.class.getResource("/graphics/exit.png")));
		exitButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				Object[] options = {"כן","לא"};
        		int n = JOptionPane.showOptionDialog(myself, "האם ברצונך לצאת מהמערכת?","אישור יציאה", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,"");
        		
        		if (n==0)
        		{
        			if (VPOfOperationsMain.voopClient != null)
                    {
                      try
                      {
                    	  VPOfOperationsMain.voopClient.close();
                      }
                      catch (Exception ignore)
                      {}
                     }
            		
            		System.exit(0);
        		}
        		
			}
		});
		add(exitButton, "cell 1 2,alignx center");
		
		
		
		
		
	}
	
	/**
	 * Fetch data for the dashboard and it's components
	 */
	public ArrayList<Object> getDatesForDashboard()
	{
		ArrayList<Object> answer = new ArrayList<Object>();

		
		UtilDateModel todaymodel = new UtilDateModel();
		
		todaymodel.setDate(todaymodel.getYear(), todaymodel.getMonth(), (todaymodel.getDay()));
	

		todaymodel.addDay(-1);
		
		
		todaymodel.setSelected(true);
		
	
		
		answer.add(startDate.getValue());
		answer.add(endDate.getValue());
		answer.add(todaymodel.getValue()); // yesterday's day
		
		
		return answer;
		
	}
	
	/**
	 * Set GUI fields data
	 */
	public void setVariables(ArrayList<Object> dashboardData)
	{
		
		// Handle Gauges
		
				String gaugesDataFlag = dashboardData.get(0).toString();
				String wipFlag = dashboardData.get(1).toString();
				String failReportFlag = dashboardData.get(2).toString();
				
				if (gaugesDataFlag.equalsIgnoreCase("valid"))
				{
					
					DashboardGauges gaugesData = (DashboardGauges)((ArrayList<Object>)dashboardData.get(3)).get(1);
					
					// Engrave Gauge
					engraveDoneTextField.setText(String.valueOf(gaugesData.getEngraveReported()));
					engravePlanTextField.setText(String.valueOf(gaugesData.getEngraveTarget()));
					engraveYesterdayTextField.setText(String.valueOf(gaugesData.getYesterdayEngraveReported()));
					
					double engraveGaugeData;
					if (engravePlanTextField.getText().equals("0"))
					{
						// don't divide by zero, set gauge to 0
						engraveGaugeData=0;
					}
					else
					{
						 engraveGaugeData = (Double.valueOf(engraveDoneTextField.getText()) / Double.valueOf(engravePlanTextField.getText()))*100; 
						 
					}
					VPOfOperationsMain.dashboardEngraveGauge.setValueAnimated(engraveGaugeData);
					
					
					// Polish Gauge
					polishDoneTextField.setText(String.valueOf(gaugesData.getPolishReported()));
					polishPlanTextField.setText(String.valueOf(gaugesData.getPolishTarget()));
					polishYesterdayTextField.setText(String.valueOf(gaugesData.getYesterdayPolishReported()));
					
					double polishGaugeData;
					if (polishPlanTextField.getText().equals("0"))
					{
						// don't divide by zero, set gauge to 0
						polishGaugeData=0;
					}
					else
					{
						polishGaugeData = (Double.valueOf(polishDoneTextField.getText()) / Double.valueOf(polishPlanTextField.getText()))*100; 
					
					}
					VPOfOperationsMain.dashboardPolishGauge.setValueAnimated(polishGaugeData);
					
					
					// Quality Control Gauge
					
					qualityDoneTextField.setText(String.valueOf(gaugesData.getQcReported()));
					qualityPlanTextField.setText(String.valueOf(gaugesData.getQcTarget()));
					qualityYesterdayTextField.setText(String.valueOf(gaugesData.getYesterdayQCReported()));
					
					double qcGaugeData;
					if (qualityPlanTextField.getText().equals("0"))
					{
						// don't divide by zero, set gauge to 0
						qcGaugeData=0;
					}
					else
					{
						qcGaugeData = (Double.valueOf(qualityDoneTextField.getText()) / Double.valueOf(qualityPlanTextField.getText()))*100; 
					
					}
					VPOfOperationsMain.dashboardQcGauge.setValueAnimated(qcGaugeData);
					
					
					// Packaging Gauge
					
					packagingDoneTextField.setText(String.valueOf(gaugesData.getPackagingReported()));
					packagingPlanTextField.setText(String.valueOf(gaugesData.getPackagingTarget()));
					packagingYesterdayTextField.setText(String.valueOf(gaugesData.getYesterdayPackagingReported()));
					
					double packagingGaugeData;
					if (qualityPlanTextField.getText().equals("0"))
					{
						// don't divide by zero, set gauge to 0
						packagingGaugeData=0;
					}
					else
					{
						packagingGaugeData = (Double.valueOf(packagingDoneTextField.getText()) / Double.valueOf(packagingPlanTextField.getText()))*100; 
						
					}
					VPOfOperationsMain.dashboardPackagingGauge.setValueAnimated(packagingGaugeData);
					
					
				}
				else
				{
					// SQL Error
				}
		
		// Handle WIP
			
				if (wipFlag.equalsIgnoreCase("valid"))
				{
					WIP wipData = (WIP)((ArrayList<Object>)dashboardData.get(4)).get(1);
					wipEngraveTextField.setText(String.valueOf(wipData.getQuantityStatus1()+wipData.getQuantityStatus2()));
					wipPolishTextField.setText((String.valueOf(wipData.getQuantityStatus3() + wipData.getQuantityStatus31() + wipData.getQuantityStatus5())));
					wipQualityTextField.setText(String.valueOf(wipData.getQuantityStatus4() + wipData.getQuantityStatus7()));
					wipPackagingTextField.setText(String.valueOf(wipData.getQuantityStatus7()));
					
				}
				else
				{
					// SQL Error
				}
				
		// Handle Quality Reports
				
				if (failReportFlag.equalsIgnoreCase("valid"))
				{
					FailReport periodFailReportData = (FailReport)((ArrayList<Object>)dashboardData.get(5)).get(1);
					FailReport yesterdayFailReportData = (FailReport)((ArrayList<Object>)dashboardData.get(5)).get(2);
					
					// for period
					int totalPeriod = periodFailReportData.hardTotal()+periodFailReportData.hydrophilicTotal()+periodFailReportData.hydrophobicTotal()+periodFailReportData.presbiaTotal();
					
					
					double hardPercentage;
					if (totalPeriod == 0)
						hardPercentage= 0;
					else
						hardPercentage = (periodFailReportData.hardFails()*100.00)/totalPeriod;

					totalHardCountTextField.setText(String.valueOf(periodFailReportData.hardFails()));
					totalHardPercentageTextField.setText(String.format("%.2f", hardPercentage )+ "%");
					
					
					double hydrophilPercentage;
					if (totalPeriod == 0)
						hydrophilPercentage= 0;
					else
						hydrophilPercentage= (periodFailReportData.hydrophilicFails()*100.00)/totalPeriod;
					
					totalHydrophilCountTextField.setText(String.valueOf(periodFailReportData.hydrophilicFails()));
					totalHydrophilPercentageTextField.setText(String.format("%.2f", hydrophilPercentage ) + "%");
					
					double hydrophobPercentage;
					if (totalPeriod == 0)
						hydrophobPercentage = 0;
					else
						hydrophobPercentage = (periodFailReportData.hydrophobicFails()*100.00)/totalPeriod;
					
					totalHydrophobCountTextField.setText(String.valueOf(periodFailReportData.hydrophobicFails()));
					totalHydrophobPercentageTextField.setText(String.format("%.2f", hydrophobPercentage ) + "%");
					

					double presbiaPercentage;
					if (totalPeriod == 0)
						presbiaPercentage = 0;
					else
						presbiaPercentage = (periodFailReportData.presbiaFails()*100.00)/totalPeriod;
					totalPresbiaCountTextField.setText(String.valueOf(periodFailReportData.presbiaFails()));
					totalPresbiaPercentageTextField.setText(String.format("%.2f", presbiaPercentage ) + "%" );
					
					
					
					double totalPercentage;
					if (totalPeriod == 0)
						totalPercentage = 0;
					else
						totalPercentage = (periodFailReportData.hardFails() + periodFailReportData.hydrophilicFails() + periodFailReportData.hydrophobicFails() + periodFailReportData.presbiaFails())*100.0/totalPeriod;
					
					
					cummulatativeQualityTotalTextField.setText(String.valueOf(periodFailReportData.hardFails() + periodFailReportData.hydrophilicFails() + periodFailReportData.hydrophobicFails() + periodFailReportData.presbiaFails()));
					cummulatativeQualityPercentageTextField.setText(String.format("%.2f", totalPercentage) + "%");
					
					
					
					
					// for yesterday
					
					int totalyesterday = yesterdayFailReportData.hardTotal()+yesterdayFailReportData.hydrophilicTotal()+yesterdayFailReportData.hydrophobicTotal()+yesterdayFailReportData.presbiaTotal();
					
					
					double hardPercentageYesterday;
					if (totalyesterday == 0)
						hardPercentageYesterday= 0;
					else
						hardPercentageYesterday = (yesterdayFailReportData.hardFails()*100.00)/totalyesterday;

					yesterdayHardCountTextField.setText(String.valueOf(yesterdayFailReportData.hardFails()));
					yesterdayHardPercentageTextField.setText(String.format("%.2f", hardPercentageYesterday )+ "%");
					
					
					double hydrophilPercentageYesterday;
					if (totalyesterday == 0)
						hydrophilPercentageYesterday= 0;
					else
						hydrophilPercentageYesterday= (yesterdayFailReportData.hydrophilicFails()*100.00)/totalyesterday;
					
					yesterdayHydrophilCountTextField.setText(String.valueOf(yesterdayFailReportData.hydrophilicFails()));
					yesterdayHydrophilPercentageTextField.setText(String.format("%.2f", hydrophilPercentageYesterday ) + "%");
					
					double hydrophobPercentageYesterday;
					if (totalyesterday == 0)
						hydrophobPercentageYesterday = 0;
					else
						hydrophobPercentageYesterday = (yesterdayFailReportData.hydrophobicFails()*100.00)/totalyesterday;
					
					yesterdayHydrophobCountTextField.setText(String.valueOf(yesterdayFailReportData.hydrophobicFails()));
					yesterdayHydrophobPercentageTextField.setText(String.format("%.2f", hydrophobPercentageYesterday ) + "%");
					

					double presbiaPercentageYesterday;
					if (totalyesterday == 0)
						presbiaPercentageYesterday = 0;
					else
						presbiaPercentageYesterday = (yesterdayFailReportData.presbiaFails()*100.00)/totalyesterday;
					yesterdayPresbiaCountTextField.setText(String.valueOf(yesterdayFailReportData.presbiaFails()));
					yesterdayPresbiaPercentageTextField.setText(String.format("%.2f", presbiaPercentageYesterday ) + "%" );
					
					
					
					double totalPercentageYesterday;
					if (totalyesterday == 0)
						totalPercentageYesterday = 0;
					else
						totalPercentageYesterday = (yesterdayFailReportData.hardFails() + yesterdayFailReportData.hydrophilicFails() + yesterdayFailReportData.hydrophobicFails() + yesterdayFailReportData.presbiaFails())*100.0/totalyesterday;
					// yesterday
					
					
					yesterdayQualityTotalTextField.setText(String.valueOf(yesterdayFailReportData.hardFails() + yesterdayFailReportData.hydrophilicFails() + yesterdayFailReportData.hydrophobicFails() + yesterdayFailReportData.presbiaFails()));
					yesterdayQualityPercentageTextField.setText(String.format("%.2f", totalPercentageYesterday) + "%");

					
					
					
					
				}
				else
				{
					// SQL Error
				}
		
	}

	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
